package com.yang.token.controller;

import com.alibaba.fastjson.JSONObject;
import com.yang.token.model.User;
import com.yang.token.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class TestController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/insertUser")
    public Map<String, Object> insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    @RequestMapping("/insertUserList")
    public Map<String, Object> insertUserList(@RequestParam String userListStr,
                                              @RequestParam String userListId){

        List<User> userList = JSONObject.parseArray(userListStr, User.class);
        return userService.insertUserList(userList, userListId);
    }

    @RequestMapping("/queryAll")
    public List<User> queryAll(){
        List<User> lists = userService.queryAll();
        return lists;
    }

    @RequestMapping("/queryUserList")
    public List<User> queryUserList(@RequestParam int id){
        List<User> userList = userService.queryUserList(id);
        return userList;
    }

    @RequestMapping("/findUserById")
    public Map<String, Object> findUserById(@RequestParam int id){
        User user = userService.findUserById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("uid", user.getUid());
        result.put("uname", user.getUserName());
        result.put("pass", user.getPassWord());
        result.put("salary", user.getSalary());
        return result;
    }

    @RequestMapping("/updateUser")
    public String updateUser(){
        User user = new User();
        user.setUid(1111);
        user.setUserName("cat");
        user.setPassWord("miaomiao");
        user.setSalary(4000);

        int result = userService.updateUser(user);

        if(result != 0){
            return "update user success";
        }

        return "fail";
    }

    @RequestMapping("/deleteUserById")
    public String deleteUserById(@RequestParam int id){
        int result = userService.deleteUserById(id);
        if(result != 0){
            return "delete success";
        }
        return "delete fail";
    }

    @RequestMapping("/deleteUser")
    public Map<String, Object> deleteUser(@RequestParam int id){

        return userService.deleteUser(id);

    }

    //redis 列表
    @GetMapping("list")
    public List<String> listTest(){
        ListOperations<String,String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list","hello");
        listOperations.leftPush("list","world");
        listOperations.rightPush("list","java");

        List<String> list = listOperations.range("list",0,2);
        return list;
    }

    //redis 集合
    @GetMapping("/set")
    public Set<String> setTest(){
        SetOperations<String,String> setOperations = redisTemplate.opsForSet();
        setOperations.add("set","Hello");
        setOperations.add("set","Hello");
        setOperations.add("set","world");
        setOperations.add("set","world");
        setOperations.add("set","java");
        setOperations.add("set","java");

        Set<String> set = setOperations.members("set");
        return set;
    }

    //redis 有序集合
    @GetMapping("/zset")
    public Set<String> zsetTest(){
        ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset","Hello",1);
        zSetOperations.add("zset","world",2);
        zSetOperations.add("zset","java",3);

        Set<String> set = zSetOperations.range("zset",0,2);
        return set;
    }

    //redis Hash
    @GetMapping("/hash")
    public void hashTest(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("key","hashKey","Hello");
        System.out.println(hashOperations.get("key","hashKey"));

    }

}
